// copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
// see COPYRIGHT for reuse legalities
//


#import "WWSample.h"

@implementation WWSample

- setData:newData timestamp:(float)floatTime generator:(const char *)genName weight:(float)newWeight
{
   if (data)
   {  if (freeData)
      {  [data free];
      }
   }
   data = newData;
   freeData = YES;

   timestamp = floatTime;  // this obviously should be an object, but for now...

   if (generatorName)
   {  free(generatorName);
   }
   if (genName)
   {  generatorName = NXCopyStringBuffer(genName);
   }
   else
   {  generatorName = NULL;
   }
   weight = newWeight;

   return self;
}

- free
{
  //NXLogError("WWSample %p at time %f being free'ed\n", self, timestamp);
  //NXLogError("WWSample freeing data %s %p\n", [[data class] name], data);
  if ([self freeData])
  {  [data free];
  }
  return [super free];
}

- (float)timestamp { return timestamp; }
- sample { return data; }
- data { return data; }

- setFreeData:(BOOL)flag { freeData = flag;  return self; }
- (BOOL)freeData { return freeData; }

- (float)weight { return weight; }
- setWeight:(float)newWeight { weight = newWeight; return self; }

- (int)generatorCount { return 1; }

- (const char *)generatorName { return (const char *)generatorName; }


- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   //int  i;


   //for (i = 0; i < tab; i++)
   //{  NXPrintf(stream, "\t");
   //}

   if (generatorName)
   {  NXPrintf(stream, "{{%s} %f {", generatorName, weight);
   }
   else
   {  NXPrintf(stream, "{{} %f {", weight);
   }
   [data writeEve:stream atTabLevel:0];  // kinda gross, but we've already moved the cursor where we want...
   NXPrintf(stream, "}} ");

   return self;
}

- oldWrite3DTextScene:(NXStream *)stream atTabLevel:(int)tab index:(int)index time:(float)time
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }

   NXPrintf(stream, "startShape %s; ", [[data class] name]);
   // need tab
   // need index (position in current list)
   NXPrintf(stream, "set __text__(colorTime%f) {1 1 1};", time);
   NXPrintf(stream, "set __text__(colorSurface%f) plastic;", time);
   NXPrintf(stream, "EveCmd {Color $__text__(colorTime%f) }; ", time); // to make animation easy
   NXPrintf(stream, "EveCmd {Surface $__text__(colorSurface%f)}; ", time); // to make animation easy

   NXPrintf(stream, 
	    "EveCmd {Translate [expr { %d * $__text__(tabLength)}] ", tab);
   NXPrintf(stream, 
            "[expr {$__text__(spacingFactor) * %d * $__text__(spacing) * $__text__(fontSize)}] ", index);
   NXPrintf(stream, 
            "[expr {$__text__(sampleOffset) + ($__text__(timeFactor) * %f)}]};\n", time); 
   NXPrintf(stream, "  EveCmd {WW3DText $__text__(fontName) $__text__(fontSize) {");
     [data writeEve:stream atTabLevel:tab];
   NXPrintf(stream, "} left;}\n");
  NXPrintf(stream, "endShape;\n");

  return self;
}

- write3DTextScene:(NXStream *)stream atTabLevel:(int)tab index:(int)index time:(float)time until:(float)lastTime
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }

   NXPrintf(stream, "startShape %s; ", [[data class] name]);
   // need tab
   // need index (position in current list)
   NXPrintf(stream, "set __text__(colorTime%f) {1 1 1};", time);
   NXPrintf(stream, "set __text__(colorSurface%f) plastic;", time);
   NXPrintf(stream, "EveCmd {Color $__text__(colorTime%f) }; ", time); // to make animation easy
   NXPrintf(stream, "EveCmd {Surface $__text__(colorSurface%f)}; ", time); // to make animation easy

   NXPrintf(stream, 
	    "EveCmd {Translate [expr { %d * $__text__(tabLength)}] ", tab);
   NXPrintf(stream, 
            "[expr {$__text__(spacingFactor) * %d * $__text__(spacing) * $__text__(fontSize)}] ", index);
   NXPrintf(stream, 
            "[expr {$__text__(sampleOffset) + ($__text__(timeFactor) * %f)}]};\n", time); 
   NXPrintf(stream, "  EveCmd {WW3DText $__text__(fontName) $__text__(fontSize) {");
     [data writeEve:stream atTabLevel:tab];
   NXPrintf(stream, "} left;} {\\\n");
   // okay, now we want to put a time stamped sample of this guy out there
   // if the current timestamp isn't 0.0, we need to stick a blank a sample at 0.0

   // so here's the deal:
  
   if (timestamp == 0.0)
   { if ([self generatorName])
     {  NXPrintf(stream, "          {0.000000 { {{%s} %f {WW3DText {Helvetica} 10.0 {", [self generatorName], [self weight]);
     }
     else
     {  NXPrintf(stream, "          {0.000000 { {{} %f {WW3DText {Helvetica} 10.0 {", [self weight]);
     }
       [data writeEve:stream atTabLevel:tab];
      NXPrintf(stream, "} left;}} }} \\\n");
   }
   else
   {  if ([self generatorName])
      {  NXPrintf(stream, "          {0.000000 { {{%s} %f {WW3DText {Helvetica} 10.0 {} left;}} }} \\\n", [self generatorName], [self weight]);
         NXPrintf(stream, "          {%f { {{%s} %f {WW3DText {Helvetica} 10.0 {", [self timestamp], [self generatorName], [self weight]);
      }
      else
      {  NXPrintf(stream, "          {0.000000 { {{} %f {WW3DText {Helvetica} 10.0 {} left;}} }} \\\n", [self weight]);
         NXPrintf(stream, "          {%f { {{} %f {WW3DText {Helvetica} 10.0 {", [self timestamp], [self weight]);
      }
       [data writeEve:stream atTabLevel:tab];
      NXPrintf(stream, "} left;}} }} \\\n");
   }

   // this takes care of "turning off" this sample at the appropriate time, as long as it's not the last
   if ((timestamp != lastTime) && (lastTime != -1))  // -1 is magic for it being the last...
   {  NXPrintf(stream, "          {%f { {{%s} %f {WW3DText {Helvetica} 10.0 {} left;}} }} \\\n", (lastTime * .999), [self generatorName], [self weight]);
   }
   NXPrintf(stream, "         }\n");

  NXPrintf(stream, "endShape;\n");

  return self;
}

// boy, this is dumb... This is to get around the stupid warnings from the compiler - ask wave for details
- class { return [super class]; }
- (BOOL) conformsTo: (Protocol *)aProtocolObject { return [super conformsTo:aProtocolObject]; }

@end
